package com.qianfeng.zhangwei.sybl.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.qianfeng.zhangwei.sybl.R;
import com.qianfeng.zhangwei.sybl.adapter.AppBaseAdapter;
import com.qianfeng.zhangwei.sybl.widget.EmptyLayout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public abstract class PullToRefereshFragment<T extends Serializable> extends Fragment implements PullToRefreshBase.OnRefreshListener2<ListView>, AdapterView.OnItemClickListener, View.OnClickListener {
    public List<T> list = new ArrayList<>();
    public AppBaseAdapter<T> adapter;
    public PullToRefreshListView ptrListView;
    private EmptyLayout emptyLayout;
    protected int mCurrentPage = 1;
    private ListView listView;
    public LayoutInflater inflater;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * 获取获得布局文件
     *
     * @return
     */
    protected int getLayoutId() {
        return R.layout.fragment_pulltorefresh_list_layout;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.inflater = inflater;
        View view = inflater.inflate(R.layout.fragment_pulltorefresh_list_layout, container, false);
        return view;
    }

    /**
     * @param view
     * @param savedInstanceState
     */
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        loadData(ptrListView, adapter, mCurrentPage);
    }

    protected void initView(View view) {
        ptrListView = (PullToRefreshListView) view.findViewById(R.id.fragment_base_ptrlistview);
        emptyLayout = (EmptyLayout) view.findViewById(R.id.emptyLayout);
        emptyLayout.setOnLayoutClickListener(this);
        setEmptyView();
        listView = ptrListView.getRefreshableView();
        if (listView != null) {
            addHeadView(listView);
        }
        ptrListView.setOnRefreshListener(this);
        ptrListView.setOnItemClickListener(this);
        setAdapter(listView);
    }

    private void setEmptyView() {
        if (emptyLayout != null) {
            ptrListView.setEmptyView(emptyLayout);
            emptyLayout.setOnLayoutClickListener(this);
        }
    }


    public abstract void loadData(PullToRefreshListView ptrListView, AppBaseAdapter<T> adapter, int mCurrentPage);

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        mCurrentPage = 1;
        if (refreshView.getMode() != PullToRefreshBase.Mode.BOTH) {
            refreshView.setMode(PullToRefreshBase.Mode.BOTH);
        }
        loadData(ptrListView, adapter, mCurrentPage);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        mCurrentPage++;
        loadData(ptrListView, adapter, mCurrentPage);
    }
    public void addHeadView(ListView listView) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onClick(View v) {

    }

    public abstract void setAdapter(ListView listView);

}

